const AdminUserSevire = require('../sercice/AdminUserSevire')
const md5 = require('md5')
const config = require('../config/config')
const JWT = require("../utils/token")
const AdminUserControl = {
    login_admin: async (req, res) => {
        const { username, password } = req.body
        let token = JWT.crateJWT({ username, password })
        try {
            res.header("Access-Control-Expose-Headers", 'Token')
            res.header("Token", token)
            const result = await AdminUserSevire.login({ username, password: md5(password + config.script) })
            if (!result) {
                res.status(400).send({
                    msg: "登录失败",
                    data: "密码或用户名错误",
                    code: 400
                })
                return;
            }
            res.status(200).send({
                msg: "登录成功",
                data: result,
                code: 200,
                token: token
            })
        } catch (error) {
            console.log(error.message, '错误')
            res.status(400).send({
                msg: "登陆失败",
                data: error.message,
                code: 400
            })
        }

    },
    //修改信息
    put_admin: async (req, res) => {


        // const { username, password, newpassword } = data
        try {
            //获取请求头的token,解析当前用户和密码
            const { username, password } = JWT.verifyJWT(req.headers["token"])
            console.log(req.headers["token"])
            const result = await AdminUserSevire.put_info({ username, password: md5(password + config.script) }, { ...req.body })
            console.log(result, '123')
            if (result.matchedCount === 0) {
                res.status(411).send({
                    mes: "修改失败",
                    data: ""
                })
            }
            res.status(200).send({
                mes: "修改成功"
            })
        } catch (error) {
            console.log(error.message, '错误')
            res.status(411).send({
                mes: "修改失败",
                data: error.message
            })
        }

    },
    //更换头像
    put_avatar: async (req, res) => {

        try {

            //获取图片路径
            const filePath = req.file.path
            // 将路径\\转化为/ 'public\\images\\user\\1716630232816-664794344.png',
            const normalizedPath = filePath.replace(/\\/g, '/');
            //去掉路径中的public
            const relativePath = normalizedPath.split('public')[1];

            res.status(200).send({
                msg: "成功",
                code: 200,
                data: {
                    avatar: relativePath
                }
            })

        } catch (error) {


            res.status(411).send({
                msg: "erroe",
            })
        }
    },
    //更新密码，密保
    put_renewal: async (req, res) => {

        try {
            const { username, password } = JWT.verifyJWT(req.headers["token"])
            const { oldpass, newpass } = req.body
            console.log(username, '名字')
            const result = await AdminUserSevire.renewal_pass({ username: username, password: md5(oldpass + config.script) }, md5(newpass + config.script))
            console.log(result, md5(oldpass + config.script))
            if (result.matchedCount === 0 || result.acknowledged == false) {
                res.status(411).send({
                    msg: "修改失败",
                    data: "",
                    code: 411
                })
            } else {
                res.status(200).send({
                    msg: "修改成功",
                    data: "",
                    code: 200
                })
            }

        } catch (error) {
            res.status(411).send({
                msg: "修改失败",
                data: "",
                code: 411
            })
        }
    },
    //添加超级管理员
    add_admin: async (req, res) => {
        try {
            let username = 'admin'
            let password = md5('123456' + config.script)
            const result = await AdminUserSevire.add({ username, password })
            console.log(result)
            res.status(200).send({
                msg: "添加成功",
                data: result,
                code: 200
            })

        } catch (error) {
            res.status(411).send({
                msg: "添加失败",
                data: '',
                code: 411
            })
        }
    }

}



module.exports = AdminUserControl